package xin.clips.web.controller.socket;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xin.clips.common.annotation.Log;
import xin.clips.common.annotation.VisistLog;
import xin.clips.common.constant.UserConstants;
import xin.clips.common.core.controller.BaseController;
import xin.clips.common.core.domain.AjaxResult;
import xin.clips.common.core.domain.Ztree;
import xin.clips.common.core.domain.entity.SysDept;
import xin.clips.common.core.domain.entity.SysRole;
import xin.clips.common.core.domain.entity.SysUser;
import xin.clips.common.core.page.TableDataInfo;
import xin.clips.common.core.text.Convert;
import xin.clips.common.enums.BusinessType;
import xin.clips.common.utils.ShiroUtils;
import xin.clips.common.utils.StringUtils;
import xin.clips.common.utils.poi.ExcelUtil;
import xin.clips.framework.shiro.service.SysPasswordService;
import xin.clips.framework.shiro.util.AuthorizationUtils;
import xin.clips.system.service.ISysDeptService;
import xin.clips.system.service.ISysPostService;
import xin.clips.system.service.ISysRoleService;
import xin.clips.system.service.ISysUserService;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息
 *
 * @author clips
 */
@Controller
@RequestMapping("/system/socket")
public class SocketController extends BaseController {


    @PostMapping("/go")
    @ResponseBody
    public Boolean go(Position position)  {
        String jsonStr = JSON.toJSONString(position);
        try {
            //1、创建客户端的Socket对象
            Socket socket = new Socket("localhost", 9527);

            //2、获取输出流，写数据
            OutputStream os = socket.getOutputStream();

            os.write(jsonStr.getBytes());

            //3、释放资源
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    @PostMapping("/start")
    @ResponseBody
    public Boolean start()  {
        try {
            //1、创建客户端的Socket对象
            Socket socket = new Socket("localhost", 9527);

            //2、获取输出流，写数据
            OutputStream os = socket.getOutputStream();

            String start = "start";
            String jsonStr = JSON.toJSONString(start);
            os.write(jsonStr.getBytes());

            //3、释放资源
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    @PostMapping("/stop")
    @ResponseBody
    public Boolean stop()  {
        try {
            //1、创建客户端的Socket对象
            Socket socket = new Socket("localhost", 9527);

            //2、获取输出流，写数据
            OutputStream os = socket.getOutputStream();

            String stop = "stop";
            String jsonStr = JSON.toJSONString(stop);
            os.write(jsonStr.getBytes());

            //3、释放资源
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }


}
